unit frmDebug;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, uWorkbench, StdCtrls, uDelphiBaseForm, uHHUtils,
  uLangConst, RzButton, IniFiles, RzRadChk, frmTeacherDetect, Mask, RzEdit;

const
  TEACHER_STAY_AREA1: Integer = 0;
  TEACHER_STAY_AREA2: Integer = 1;
  WHITE_BOARD_AREA: Integer = 2;

type
  TDebugForm = class(TDelphiBaseForm)
    pnlInfo: TPanel;
    pgcDebug: TPageControl;
    tsTeacher: TTabSheet;
    pnlTeacherDebug: TPanel;
    pnlTeacherTop: TPanel;
    tsStudent: TTabSheet;
    pnlStudentDebug: TPanel;
    pnlStudentHeight: TPanel;
    tsPTZ: TTabSheet;
    pnlPTZDebug: TPanel;
    pnlTFar: TPanel;
    Panel4: TPanel;
    Panel6: TPanel;
    Panel7: TPanel;
    pnlTNear: TPanel;
    Panel5: TPanel;
    btnUp: TButton;
    btnDown: TButton;
    btnLeft: TButton;
    btnRight: TButton;
    btnZoomIn: TButton;
    btnZoomOut: TButton;
    btnLeftLimit: TButton;
    btnCallLeftLimit: TButton;
    btnStart: TButton;
    btnStop: TButton;
    btnCallRightLimit: TButton;
    btnRightLimit: TButton;
    tsStudentPTZ: TTabSheet;
    Panel1: TPanel;
    pnlSFar: TPanel;
    pnlSNear: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Panel9: TPanel;
    Button13: TButton;
    Button14: TButton;
    Button15: TButton;
    Button16: TButton;
    Panel8: TPanel;
    Panel10: TPanel;
    Panel11: TPanel;
    Panel12: TPanel;
    pnlBoardAreaModify: TPanel;
    btnClearBoardArea: TRzButton;
    btnSetBoardArea: TRzButton;
    pnlWhiteBoardTop: TPanel;
    pnlWhiteBoardLeft: TPanel;
    pnlWhiteBoardRight: TPanel;
    pnlWhiteBoardBottom: TPanel;
    chkEnableCB: TCheckBox;
    Panel14: TPanel;
    btnSetStudentRect: TRzButton;
    pnlStudentTop: TPanel;
    pnlTeacherStayArea1Bottom: TPanel;
    pnlTeacherStayArea1Left: TPanel;
    pnlTeacherStayArea1Right: TPanel;
    pnlTeacherStayArea1Top: TPanel;
    pnlTeacherStayArea2Bottom: TPanel;
    pnlTeacherStayArea2Left: TPanel;
    pnlTeacherStayArea2Right: TPanel;
    pnlTeacherStayArea2Top: TPanel;
    rbTeacherStayArea1: TRzRadioButton;
    rbTeacherStayArea2: TRzRadioButton;
    rbWhiteBoardArea: TRzRadioButton;
    pnlTeacherHeight: TPanel;
    btnTeacherDetect: TButton;
    tsTeacherNear: TTabSheet;
    Panel2: TPanel;
    RzButton1: TRzButton;
    pnlTeacherNearView: TPanel;
    editVirtualY: TRzNumericEdit;
    Label8: TLabel;
    lblTPtzSpeed: TLabel;
    edtTPtzSpeed: TEdit;
    chkEnableTDetect: TCheckBox;
    chkUniformStudentSwitch: TCheckBox;
    chkUserStudentNear: TCheckBox;
    chkEnableStudentDetect: TCheckBox;
    chkTeacherSwitchMode: TCheckBox;
    btnTeacherSave: TButton;
    btnStudentSave: TButton;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    procedure pgcDebugChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnUpMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnUpMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnLeftMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnRightMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnDownMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnZoomInMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnZoomOutMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure btnLeftLimitClick(Sender: TObject);
    procedure btnRightLimitClick(Sender: TObject);
    procedure btnCallLeftLimitClick(Sender: TObject);
    procedure btnCallRightLimitClick(Sender: TObject);
    procedure Button1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button4MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button7Click(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure Button16Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button14Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure edthpllKeyPress(Sender: TObject; var Key: Char);
    procedure pnlTeacherDebugMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pnlTeacherDebugMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure pnlTeacherDebugMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnSetBoardAreaClick(Sender: TObject);
    procedure btnClearBoardAreaClick(Sender: TObject);
    procedure btnSetStudentRectClick(Sender: TObject);
    procedure rbTeacherStayArea1Click(Sender: TObject);
    procedure rbTeacherStayArea2Click(Sender: TObject);
    procedure rbWhiteBoardAreaClick(Sender: TObject);
    procedure btnTeacherDetectClick(Sender: TObject);
    procedure RzButton1Click(Sender: TObject);
  private
    FWorkbench: IMMWorkbench;

    FWhiteBoardLeftButtonDown: Boolean;
    FWhiteBoardArea: TRect;
    FTeacherStayArea1: TRect;
    FTeacherStayArea2: TRect;
    FCurrentArea: TRect;

    FStudentLeftButtonDown: Boolean;
    FStudentArea: TRect;

    FShowVGAErrorOnce: Boolean;

    FDrawStyle: Integer;

    FTeacherTop: Double;
    FTeacherHeight: Double;

    FStudentTop: Double;
    FStudentHeight: Double;
    procedure LoadSetting();
    procedure SaveSetting();
    procedure AdjustWhiteBoardArea(AArea: TRect);
    procedure AdjustStudentArea(AArea: TRect);

    procedure AdjustSelfDrawArea(AArea: TRect; ADrawStyle: Integer);
  public
    procedure SetWorkbench(AWorkbench: IMMWorkbench);
    procedure SetDebugTeacherArea(ATop: Double; AHeight: Double);
    procedure SetDebugStudentArea(ATop: Double; AHeight: Double);
    procedure ChangeLanguage(ALanguageIniFile: string); override;
  end;

var
  DebugForm: TDebugForm;

implementation

{$R *.dfm}

{ TDebugForm }

procedure TDebugForm.AdjustSelfDrawArea(AArea: TRect; ADrawStyle: Integer);
var
  pnlTop, pnlLeft, pnlRight, pnlBottom: TPanel;
begin
  if ADrawStyle = TEACHER_STAY_AREA1 then
  begin
    pnlTop := pnlTeacherStayArea1Top;
    pnlLeft := pnlTeacherStayArea1Left;
    pnlRight := pnlTeacherStayArea1Right;
    pnlBottom := pnlTeacherStayArea1Bottom;
  end;

  if ADrawStyle = TEACHER_STAY_AREA2 then
  begin
    pnlTop := pnlTeacherStayArea2Top;
    pnlLeft := pnlTeacherStayArea2Left;
    pnlRight := pnlTeacherStayArea2Right;
    pnlBottom := pnlTeacherStayArea2Bottom;
  end;

  if ADrawStyle = WHITE_BOARD_AREA then
  begin
    pnlTop := pnlWhiteBoardTop;
    pnlLeft := pnlWhiteBoardLeft;
    pnlRight := pnlWhiteBoardRight;
    pnlBottom := pnlWhiteBoardBottom;
  end;

  pnlTop.Width := AArea.Right - AArea.Left;
  pnlTop.Left := AArea.Left;
  pnlTop.Top := AArea.Top;

  pnlLeft.Height := AArea.Bottom - AArea.Top;
  pnlLeft.Left := AArea.Left;
  pnlLeft.Top := AArea.Top;

  pnlBottom.Width := AArea.Right - AArea.Left;
  pnlBottom.Left := AArea.Left;
  pnlBottom.Top := AArea.Bottom;

  pnlRight.Height := AArea.Bottom - AArea.Top;
  pnlRight.Left := AArea.Right;
  pnlRight.Top := AArea.Top;
end;

procedure TDebugForm.AdjustStudentArea(AArea: TRect);
begin
end;

procedure TDebugForm.AdjustWhiteBoardArea(AArea: TRect);
begin
  pnlWhiteBoardTop.Width := AArea.Right - AArea.Left;
  pnlWhiteBoardTop.Left := AArea.Left;
  pnlWhiteBoardTop.Top := AArea.Top;

  pnlWhiteBoardLeft.Height := AArea.Bottom - AArea.Top;
  pnlWhiteBoardLeft.Left := AArea.Left;
  pnlWhiteBoardLeft.Top := AArea.Top;

  pnlWhiteBoardBottom.Width := AArea.Right - AArea.Left;
  pnlWhiteBoardBottom.Left := AArea.Left;
  pnlWhiteBoardBottom.Top := AArea.Bottom;

  pnlWhiteBoardRight.Height := AArea.Bottom - AArea.Top;
  pnlWhiteBoardRight.Left := AArea.Right;
  pnlWhiteBoardRight.Top := AArea.Top;
end;

procedure TDebugForm.btnCallLeftLimitClick(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('CallTeacherCameraLeftLimit');
end;

procedure TDebugForm.btnCallRightLimitClick(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('CallTeacherCameraRightLimit');
end;

procedure TDebugForm.btnDownMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZTeacherCmd?3');
end;

procedure TDebugForm.btnLeftLimitClick(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('SetTeacherCameraLeftLimit');
end;

procedure TDebugForm.btnLeftMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZTeacherCmd?4');
end;

procedure TDebugForm.btnClearBoardAreaClick(Sender: TObject);
begin
  ZeroMemory(@FTeacherStayArea1, SizeOf(TRect));
  ZeroMemory(@FTeacherStayArea2, SizeOf(TRect));
  ZeroMemory(@FWhiteBoardArea, SizeOf(TRect));
  AdjustSelfDrawArea(FWhiteBoardArea, WHITE_BOARD_AREA);
  AdjustSelfDrawArea(FTeacherStayArea1, TEACHER_STAY_AREA1);
  AdjustSelfDrawArea(FTeacherStayArea2, TEACHER_STAY_AREA2);

  FWorkbench.SendHttpCmd('SetWhiteBoardArea?0&0&0&0&0');
  FWorkbench.SendHttpCmd('SetTeacherStayArea1?0&0&0&0&0');
  FWorkbench.SendHttpCmd('SetTeacherStayArea2?0&0&0&0&0');
    
//  AdjustWhiteBoardArea(FWhiteBoardArea);
end;

procedure TDebugForm.btnRightLimitClick(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('SetTeacherCameraRightLimit');
end;

procedure TDebugForm.btnRightMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZTeacherCmd?2');
end;

procedure TDebugForm.btnSetBoardAreaClick(Sender: TObject);
var
  x, y, width, height: Single;
begin
  if (FWhiteBoardArea.Right - FWhiteBoardArea.Left) < pnlTeacherDebug.Width / 10 then
  begin
    ZeroMemory(@FWhiteBoardArea, SizeOf(TRect));
    AdjustSelfDrawArea(FWhiteBoardArea, WHITE_BOARD_AREA);
  end
  else
  begin
    x := FWhiteBoardArea.Left / pnlTeacherDebug.Width;
    y := FWhiteBoardArea.Top / pnlTeacherDebug.Height;
    width := (FWhiteBoardArea.Right - FWhiteBoardArea.Left) / pnlTeacherDebug.Width;
    height := (FWhiteBoardArea.Bottom - FWhiteBoardArea.Top) / pnlTeacherDebug.Height;

    FWorkbench.SendHttpCmd(Format('SetWhiteBoardArea?%.3f&%.3f&%.3f&%.3f&%d',
      [x, y, width, height, Integer(chkEnableCB.Checked)]));
    SaveSetting();
  end;

  if (FTeacherStayArea1.Right - FTeacherStayArea1.Left) < pnlTeacherDebug.Width / 10 then
  begin
    ZeroMemory(@FTeacherStayArea1, SizeOf(TRect));
    AdjustSelfDrawArea(FTeacherStayArea1, TEACHER_STAY_AREA1);
  end
  else
  begin
    x := FTeacherStayArea1.Left / pnlTeacherDebug.Width;
    y := FTeacherStayArea1.Top / pnlTeacherDebug.Height;
    width := (FTeacherStayArea1.Right - FTeacherStayArea1.Left) / pnlTeacherDebug.Width;
    height := (FTeacherStayArea1.Bottom - FTeacherStayArea1.Top) / pnlTeacherDebug.Height;

    FWorkbench.SendHttpCmd(Format('SetTeacherStayArea1?%.3f&%.3f&%.3f&%.3f&%d',
      [x, y, width, height, Integer(chkEnableCB.Checked)]));
    SaveSetting();
  end;

  if (FTeacherStayArea2.Right - FTeacherStayArea2.Left) < pnlTeacherDebug.Width / 10 then
  begin
    ZeroMemory(@FTeacherStayArea2, SizeOf(TRect));
    AdjustSelfDrawArea(FTeacherStayArea2, TEACHER_STAY_AREA2);
  end
  else
  begin
    x := FTeacherStayArea2.Left / pnlTeacherDebug.Width;
    y := FTeacherStayArea2.Top / pnlTeacherDebug.Height;
    width := (FTeacherStayArea2.Right - FTeacherStayArea2.Left) / pnlTeacherDebug.Width;
    height := (FTeacherStayArea2.Bottom - FTeacherStayArea2.Top) / pnlTeacherDebug.Height;

    FWorkbench.SendHttpCmd(Format('SetTeacherStayArea2?%.3f&%.3f&%.3f&%.3f&%d',
      [x, y, width, height, Integer(chkEnableCB.Checked)]));
    SaveSetting();
  end;
end;

procedure TDebugForm.btnSetStudentRectClick(Sender: TObject);
var
  ftop, fheight: Double;
begin
  with TTeacherDetecetForm.Create(nil) do
  begin
    edtTop.Text := FloatToStr(FStudentTop);
    edtHeight.Text := FloatToStr(FStudentHeight);
    if ShowModal = 1 then
    begin
      ftop := StrToFloatDef(edtTop.Text, 0.06);
      fheight := StrToFloatDef(edtHeight.Text, 0.52);
      if (ftop > 0) and (fheight > 0) and (ftop + fheight < 1) then
      begin
        FStudentTop := ftop;
        FStudentHeight := fheight;
        SetDebugStudentArea(FStudentTop, FStudentHeight);

        FWorkbench.SendHttpCmd(Format('SetStudentDetectParam?%f&%f', [FStudentTop,
          FStudentHeight]));
      end;
    end;
  end;
end;

procedure TDebugForm.btnStartClick(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('FiveTeacherCameraStartMove');
end;

procedure TDebugForm.btnStopClick(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('FiveTeacherCameraStopMove');
end;

procedure TDebugForm.btnTeacherDetectClick(Sender: TObject);
var
  ftop, fheight: Double;
begin
  with TTeacherDetecetForm.Create(nil) do
  begin
    edtTop.Text := FloatToStr(FTeacherTop);
    edtHeight.Text := FloatToStr(FTeacherHeight);
    if ShowModal = 1 then
    begin
      ftop := StrToFloatDef(edtTop.Text, 0.06);
      fheight := StrToFloatDef(edtHeight.Text, 0.52);
      if (ftop > 0) and (fheight > 0) and (ftop + fheight < 1) then
      begin
        FTeacherTop := ftop;
        FTeacherHeight := fheight;
        SetDebugTeacherArea(FTeacherTop, FTeacherHeight);

        FWorkbench.SendHttpCmd(Format('SetTeacherDetectParam?%f&%f', [FTeacherTop,
          FTeacherHeight]));
      end;
    end;
  end;
end;

procedure TDebugForm.btnUpMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZTeacherCmd?1');
end;

procedure TDebugForm.btnUpMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZTeacherCmd?0');
end;

procedure TDebugForm.btnZoomInMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZTeacherCmd?5');
end;

procedure TDebugForm.btnZoomOutMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZTeacherCmd?6');
end;

procedure TDebugForm.Button10Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('FiveStudentCameraStopMove');
end;

procedure TDebugForm.Button11Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('CallStudentTopLeft');
end;

procedure TDebugForm.Button12Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('SetStudentTopLeft');
end;

procedure TDebugForm.Button13Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('CallStudentTopRight');
end;

procedure TDebugForm.Button14Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('CallStudentBottomRight');
end;

procedure TDebugForm.Button15Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('SetStudentBottomRight');
end;

procedure TDebugForm.Button16Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('SetStudentTopRight');
end;

procedure TDebugForm.Button1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZStudentCmd?1');
end;

procedure TDebugForm.Button1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZStudentCmd?0');
end;

procedure TDebugForm.Button2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZStudentCmd?3');
end;

procedure TDebugForm.Button3MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZStudentCmd?4');
end;

procedure TDebugForm.Button4MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FWorkbench.SendHttpCmd('PTZStudentCmd?2');
end;

procedure TDebugForm.Button7Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('SetStudentBottomLeft');
end;

procedure TDebugForm.Button8Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('CallStudentBottomLeft');
end;

procedure TDebugForm.Button9Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd('FiveStudentCameraStartMove');
end;

procedure TDebugForm.ChangeLanguage(ALanguageIniFile: string);
begin
  inherited ChangeLanguage(ALanguageIniFile);
end;

procedure TDebugForm.edthpllKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', #8]) then
    Key := #0
end;

procedure TDebugForm.FormShow(Sender: TObject);
var
  ls: TStringList;
begin
  FTeacherTop := 0.06;
  FTeacherHeight := 0.52;

  FStudentTop := 0.06;
  FStudentHeight := 0.52;

  FDrawStyle := TEACHER_STAY_AREA1;

  rbTeacherStayArea1.Checked := True;
  FShowVGAErrorOnce := False;
  FWhiteBoardLeftButtonDown := False;
  FStudentLeftButtonDown := False;
  
  Self.pgcDebug.ActivePageIndex := 0;
  pgcDebugChange(nil);
  TranslateConsts(getLanguageFileName());
  ChangeLanguage(getLanguageFileName());
  LoadSetting();
  FillChar(FWhiteBoardArea, SizeOf(FWhiteBoardArea), 0);
  FillChar(FStudentArea, SizeOf(FStudentArea), 0);

  ls := TStringList.Create;
  try
    ls.Delimiter := ';';
    ls.DelimitedText := FWorkbench.SendHttpCmd(Format('GetWhiteBoardArea?val=%d', [GetTickCount()]));
    if ls.Count >= 5 then
    begin
      FWhiteBoardArea.Left := Round(StrToFloat(ls[0]) * pnlTeacherDebug.Width);
      FWhiteBoardArea.Top := Round(StrToFloat(ls[1]) * pnlTeacherDebug.Height);
      FWhiteBoardArea.Right := FWhiteBoardArea.Left + Round(StrToFloat(ls[2]) * pnlTeacherDebug.Width);
      FWhiteBoardArea.Bottom := FWhiteBoardArea.Top + Round(StrToFloat(ls[3]) * pnlTeacherDebug.Height);
      chkEnableCB.Checked := StrToBool(ls[4]);
    end;
  finally
    ls.Free;
  end;

  ls := TStringList.Create;
  try
    ls.Delimiter := ';';
    ls.DelimitedText := FWorkbench.SendHttpCmd(Format('GetTeacherStayArea1?val=%d', [GetTickCount()]));
    if ls.Count >= 5 then
    begin
      FTeacherStayArea1.Left := Round(StrToFloat(ls[0]) * pnlTeacherDebug.Width);
      FTeacherStayArea1.Top := Round(StrToFloat(ls[1]) * pnlTeacherDebug.Height);
      FTeacherStayArea1.Right := FTeacherStayArea1.Left + Round(StrToFloat(ls[2]) * pnlTeacherDebug.Width);
      FTeacherStayArea1.Bottom := FTeacherStayArea1.Top + Round(StrToFloat(ls[3]) * pnlTeacherDebug.Height);
      chkEnableCB.Checked := StrToBool(ls[4]);
    end;
  finally
    ls.Free;
  end;

  ls := TStringList.Create;
  try
    ls.Delimiter := ';';
    ls.DelimitedText := FWorkbench.SendHttpCmd(Format('GetTeacherStayArea2?val=%d', [GetTickCount()]));
    if ls.Count >= 5 then
    begin
      FTeacherStayArea2.Left := Round(StrToFloat(ls[0]) * pnlTeacherDebug.Width);
      FTeacherStayArea2.Top := Round(StrToFloat(ls[1]) * pnlTeacherDebug.Height);
      FTeacherStayArea2.Right := FTeacherStayArea2.Left + Round(StrToFloat(ls[2]) * pnlTeacherDebug.Width);
      FTeacherStayArea2.Bottom := FTeacherStayArea2.Top + Round(StrToFloat(ls[3]) * pnlTeacherDebug.Height);
      chkEnableCB.Checked := StrToBool(ls[4]);
    end;
  finally
    ls.Free;
  end;

  AdjustSelfDrawArea(FWhiteBoardArea, WHITE_BOARD_AREA);
  AdjustSelfDrawArea(FTeacherStayArea1, TEACHER_STAY_AREA1);
  AdjustSelfDrawArea(FTeacherStayArea2, TEACHER_STAY_AREA2);

//  AdjustWhiteBoardArea(FWhiteBoardArea);

  ls := TStringList.Create;
  try
    ls.Delimiter := ';';
    ls.DelimitedText := FWorkbench.SendHttpCmd(Format('GetStudentArea?val=%d', [GetTickCount()]));
    if ls.Count >= 5 then
    begin
      FStudentArea.Left := Round(StrToFloat(ls[0]) * pnlStudentDebug.Width);
      FStudentArea.Top := Round(StrToFloat(ls[1]) * pnlStudentDebug.Height);
      FStudentArea.Right := FStudentArea.Left + Round(StrToFloat(ls[2]) * pnlStudentDebug.Width);
      FStudentArea.Bottom := FStudentArea.Top + Round(StrToFloat(ls[3]) * pnlStudentDebug.Height);
    end;
  finally
    ls.Free;
  end;

  ls := TStringList.Create;
  try
    ls.Delimiter := ';';
    ls.DelimitedText := FWorkbench.SendHttpCmd(Format('GetVirtualY?val=%d', [GetTickCount()]));
    if ls.Count >= 1 then
    begin
      editVirtualY.Text := ls[0];
    end;
  finally
    ls.Free;
  end;


  ls := TStringList.Create;
  try
    ls.Delimiter := ';';
    ls.DelimitedText := FWorkbench.SendHttpCmd(Format('GetTeacherDetectParam?val=%d', [GetTickCount()]));
    if ls.Count >= 2 then
    begin
      FTeacherTop := StrToFloatDef(ls.Strings[0], 0.06);
      FTeacherHeight := StrToFloatDef(ls.Strings[1], 0.52);
    end;
  finally
    ls.Free;
  end;

  SetDebugTeacherArea(FTeacherTop, FTeacherHeight);

  ls := TStringList.Create;
  try
    ls.Delimiter := ';';
    ls.DelimitedText := FWorkbench.SendHttpCmd(Format('GetStudentDetectParam?val=%d', [GetTickCount()]));
    if ls.Count >= 2 then
    begin
      FStudentTop := StrToFloatDef(ls.Strings[0], 0.06);
      FStudentHeight := StrToFloatDef(ls.Strings[1], 0.52);
    end;
  finally
    ls.Free;
  end;
  SetDebugStudentArea(FStudentTop, FStudentHeight);

//  pnlTeacherTop.Top := Round(pnlTeacherDebug.Height * FTeacherTop);
//  pnlTeacherHeight.Top := pnlTeacherTop.Top + Round(pnlTeacherDebug.Height * FTeacherHeight);

  AdjustStudentArea(FStudentArea);
end;


procedure TDebugForm.LoadSetting;
var
  Ini: TIniFile;
begin
//  Ini := TIniFile.Create(AppPath + AppName + '.ini');
//  try
//    FWhiteBoardArea.Left := Ini.ReadInteger('WhiteBoardArea', 'Left', 0);
//    FWhiteBoardArea.Top := Ini.ReadInteger('WhiteBoardArea', 'Top', 0);
//    FWhiteBoardArea.Right := Ini.ReadInteger('WhiteBoardArea', 'Right', 0);
//    FWhiteBoardArea.Bottom := Ini.ReadInteger('WhiteBoardArea', 'Bottom', 0); 
//  finally
//    Ini.Free;
//  end;
//  AdjustWhiteBoardArea(FWhiteBoardArea);
end;

procedure TDebugForm.pgcDebugChange(Sender: TObject);
begin
  if pgcDebug.ActivePageIndex = 0 then
  begin
    FWorkbench.SetTeacherFarCaptuerDrawHwnd(pnlTeacherDebug.Handle);
    pnlInfo.Caption := LC_GENERAL_DEBUG_TEACHER;
  end;
  
  if pgcDebug.ActivePageIndex = 1 then
  begin
    FWorkbench.SetStudentFarCaptuerDrawHwnd(pnlStudentDebug.Handle);
    pnlInfo.Caption := LC_GENERAL_DEBUG_STUDENT;
  end;

  if pgcDebug.ActivePageIndex = 2 then
  begin
    FWorkbench.SetTeacherFarCaptuerDrawHwnd(pnlTFar.Handle);
    FWorkbench.SetTeacherNearCaptuerDrawHwnd(pnlTNear.Handle);
    pnlInfo.Caption := LC_GENERAL_DEBUG_PTZ;
  end;

  if pgcDebug.ActivePageIndex = 3 then
  begin
    FWorkbench.SetStudentFarCaptuerDrawHwnd(pnlSFar.Handle);
    FWorkbench.SetStudentNearCaptuerDrawHwnd(pnlSNear.Handle);
  end;

  if pgcDebug.ActivePageIndex = 4 then
  begin
    FWorkbench.SetTeacherNearCaptuerDrawHwnd(pnlTeacherNearView.Handle);
  end;

end;

procedure TDebugForm.pnlTeacherDebugMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
    FWhiteBoardLeftButtonDown := True;

  if FDrawStyle = TEACHER_STAY_AREA1 then
    FCurrentArea := FTeacherStayArea1;
    
  if FDrawStyle = TEACHER_STAY_AREA2 then
    FCurrentArea := FTeacherStayArea2;

  if FDrawStyle = WHITE_BOARD_AREA then
    FCurrentArea := FWhiteBoardArea;

  FCurrentArea.Left := X;
  FCurrentArea.Top := Y;
  FCurrentArea.Right := X;
  FCurrentArea.Bottom := Y;
end;

procedure TDebugForm.pnlTeacherDebugMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  if FWhiteBoardLeftButtonDown then
  begin
    if X < Self.pnlTeacherDebug.Width then
      FCurrentArea.Right := X;
    if Y < pnlTeacherDebug.Height then
      FCurrentArea.Bottom := Y;
    AdjustSelfDrawArea(FCurrentArea, FDrawStyle);
  end;
end;

procedure TDebugForm.pnlTeacherDebugMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FWhiteBoardLeftButtonDown := False;

  if FDrawStyle = TEACHER_STAY_AREA1 then
    FTeacherStayArea1 := FCurrentArea;

  if FDrawStyle = TEACHER_STAY_AREA2 then
    FTeacherStayArea2 := FCurrentArea;

  if FDrawStyle = WHITE_BOARD_AREA then
    FWhiteBoardArea := FCurrentArea;
end;

procedure TDebugForm.rbTeacherStayArea1Click(Sender: TObject);
begin
  FDrawStyle := TEACHER_STAY_AREA1;
end;

procedure TDebugForm.rbTeacherStayArea2Click(Sender: TObject);
begin
  FDrawStyle := TEACHER_STAY_AREA2;
end;

procedure TDebugForm.rbWhiteBoardAreaClick(Sender: TObject);
begin
  FDrawStyle := WHITE_BOARD_AREA;
end;

procedure TDebugForm.RzButton1Click(Sender: TObject);
begin
  FWorkbench.SendHttpCmd(Format('SetVirtualY?%s',
    [editVirtualY.Text]));
end;

procedure TDebugForm.SaveSetting;
var
  Ini: TIniFile;
begin
//  Ini := TIniFile.Create(AppPath + AppName + '.ini');
//  try
//    Ini.WriteFloat('WhiteBoardArea', 'Left', FWhiteBoardArea.Left);
//    Ini.WriteFloat('WhiteBoardArea', 'Top', FWhiteBoardArea.Top);
//    Ini.WriteFloat('WhiteBoardArea', 'Right', FWhiteBoardArea.Right);
//    Ini.WriteFloat('WhiteBoardArea', 'Bottom', FWhiteBoardArea.Bottom);
//  finally
//    Ini.Free;
//  end;
end;

procedure TDebugForm.SetDebugStudentArea(ATop, AHeight: Double);
begin
  pnlStudentTop.Top := Round(pnlStudentDebug.Height * ATop);
  pnlStudentHeight.Top := pnlStudentTop.Top + Round(pnlStudentDebug.Height * AHeight);
end;

procedure TDebugForm.SetDebugTeacherArea(ATop: Double; AHeight: Double);
begin
  pnlTeacherTop.Top := Round(pnlTeacherDebug.Height * ATop);
  pnlTeacherHeight.Top := pnlTeacherTop.Top + Round(pnlTeacherDebug.Height * AHeight);
end;

procedure TDebugForm.SetWorkbench(AWorkbench: IMMWorkbench);
begin
  FWorkbench := AWorkbench;
end;

end.
